package pprof

import (
	"fmt"
	"net"
	"net/http"
	_ "net/http/pprof"
	"strings"
)

func InitPprof() {
	Init("")
}
func Init(port string) {
	var err error
	var listen net.Listener
	var address = "0.0.0.0"
	if port != "" {
		address = fmt.Sprintf("%s:%s", address, port)
	} else {
		address = fmt.Sprintf("%s:%d", address, 0) // 使用空闲端口
	}
	listen, err = net.Listen("tcp", address)
	if err != nil {
		return
	}
	// 性能分析
	go func() {
		if err := http.Serve(listen, nil); err != nil {
			//logger.Error("%s", err)
		}
	}()
	pprofAddr := listen.Addr().String()
	pprofAddr = strings.Replace(pprofAddr, "[::]", "127.0.0.1", -1)

	fmt.Printf("The PProf server listen on [%s]. http://%s/debug/pprof/\n", pprofAddr, pprofAddr)
	address = pprofAddr
}
